package com.zjun.demo.experimentaldemo.audio

import android.Manifest
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.bluetooth.BluetoothProfile
import android.content.Context
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import java.lang.Thread.sleep
import kotlin.concurrent.thread

/**
 * 监听蓝牙耳机接入类
 * 需要申请
 * <uses-permission android:name="android.permission.BLUETOOTH" />
 * 才能有效工作
 */
class BluetoothHeadsetStateProvider(
    private var context: Context?,
    private var headsetIsConnected: ((Boolean)->Unit)? = null,
) {

    var isHeadsetConnected = getHeadsetState()

    private var bluetoothManager:BluetoothManager? = null

    private var mProfile: Int = -1

    private var mProxy: BluetoothProfile? = null

    private var mBluetoothProfile:BluetoothProfile.ServiceListener? = object : BluetoothProfile.ServiceListener {
        // This method will be used when the new device connects
        override fun onServiceConnected(profile: Int, proxy: BluetoothProfile?) {
            // Checking if it is the headset that's active
            if (profile == BluetoothProfile.HEADSET){
                // Refreshing state
                isHeadsetConnected = true
                mProfile = profile
                mProxy = proxy
                thread {
                    sleep(2000)
                    headsetIsConnected?.invoke(true)
                }
            }

        }

        // This method will be used when the new device disconnects
        override fun onServiceDisconnected(profile: Int) {
            if (profile == BluetoothProfile.HEADSET){
                isHeadsetConnected = false
                mProfile = -1
                mProxy = null
                thread {
                    sleep(2000)
                    headsetIsConnected?.invoke(false)
                }
            }
        }
        // Enabling ServiceListener for headsets
    }

    init {

        val bluetoothService = context!!.getSystemService(Context.BLUETOOTH_SERVICE)
        if(bluetoothService != null){
            val bluetoothManager = bluetoothService as BluetoothManager

            // Receive the adapter from BluetoothManager and install our ServiceListener
            bluetoothManager.adapter.getProfileProxy(context, mBluetoothProfile, BluetoothProfile.HEADSET)
        }
    }

    // The method of receiving the current state of the bluetooth headset. Only used to initialize the starting state
    @Suppress("MemberVisibilityCanBePrivate")
    fun getHeadsetState(): Boolean {
        // Checking if there are active headsets
        if (ActivityCompat.checkSelfPermission(context!!, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
            return false
        }
        if(bluetoothManager == null){
            return false
        }
        val adapter = bluetoothManager!!.adapter
        return adapter?.getProfileConnectionState(BluetoothProfile.HEADSET) == BluetoothAdapter.STATE_CONNECTED
    }

    fun release(){
        headsetIsConnected = null
        mBluetoothProfile = null
        mProxy?.run {
            bluetoothManager?.adapter?.closeProfileProxy(mProfile,mProxy)
        }
        context = null
    }

}